﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using WeFrame.Core.Common;
using WeFrame.Core.Model;
using WeFrame.Core.BLL;

namespace WeFrame.Core.UI
{
    public class AdminPage : System.Web.UI.Page
    {
        protected internal SiteConfigModel siteConfigModel;

        public AdminPage()
        {
            this.Load += new EventHandler(ManagePage_Load);
            siteConfigModel = new SiteConfigBLL().loadConfig();
        }

        private void ManagePage_Load(object sender, EventArgs e)
        {
            //判断管理员是否登录
            if (!IsAdminLogin())
            {
                Response.Redirect("/Admin/Login.aspx");
                Response.End();
            }
        }

        #region 管理员============================================
        /// <summary>
        /// 判断管理员是否已经登录(解决Session超时问题)
        /// </summary>
        public bool IsAdminLogin()
        {
            //如果Session为Null
            if (Session[WeFrameKey.SESSION_ADMIN] != null)
            {
                return true;
            }
            else
            {
                //检查Cookies
                string adminName = Utils.GetCookie("AdminName");
                string adminPassword = Utils.GetCookie("AdminPassword");
                if (adminName != "" && adminPassword != "")
                {
                    SystemAdminBLL bll = new SystemAdminBLL();
                    SystemAdminEntity model = new SystemAdminBLL().GetModel(adminName, adminPassword);
                    if (model != null)
                    {
                        Session[WeFrameKey.SESSION_ADMIN] = model;
                        return true;
                    }
                }
            }
            return false;
        }




        /// <summary>
        /// 取得管理员信息
        /// </summary>
        public  SystemAdminEntity GetSystemAdmin()
        {
            if (IsAdminLogin())
            {
                SystemAdminEntity model = Session[WeFrameKey.SESSION_ADMIN] as SystemAdminEntity;
                if (model != null)
                {
                    return model;
                }
            }
            return null;
        }


        /// <summary>
        /// 检查管理员权限
        /// </summary>
        /// <param name="nav_name">菜单名称</param>
        /// <param name="action_type">操作类型</param>
        public void ChkAdminLevel(string nav_name, string action_type)
        {
            //Model.manager model = GetAdminInfo();
            //BLL.manager_role bll = new BLL.manager_role();
            
            SystemAdminEntity adminModel = this.GetSystemAdmin();
            SystemRoleBLL bll = new SystemRoleBLL();

            //bool result = bll.Exists(model.role_id, nav_name, action_type);
            bool result = bll.Exists(adminModel.RoleId,nav_name,action_type);

            if (!result)
            {
                string msgbox = "parent.jsdialog(\"错误提示\", \"您没有管理该页面的权限，请勿非法进入！\", \"back\")";
                Response.Write("<script type=\"text/javascript\">" + msgbox + "</script>");
                Response.End();
            }
        }

        #endregion



        #region JS提示============================================
        /// <summary>
        /// 添加编辑删除提示
        /// </summary>
        /// <param name="msgtitle">提示文字</param>
        /// <param name="url">返回地址</param>
        protected void JscriptMsg(string msgtitle, string url)
        {
            string msbox = "parent.jsprint(\"" + msgtitle + "\", \"" + url + "\")";
            ClientScript.RegisterClientScriptBlock(Page.GetType(), "JsPrint", msbox, true);
        }



        /// <summary>
        /// 添加编辑删除提示
        /// </summary>
        /// <param name="msgtitle">提示文字</param>
        /// <param name="url">返回地址</param>
        protected void SelfJscriptMsg(string msgtitle, string url)
        {
            string msbox = "parent.selfJsPrint(\"" + msgtitle + "\", \"" + url + "\")";
            ClientScript.RegisterClientScriptBlock(Page.GetType(), "selfJsPrint", msbox, true);
        }

        /// <summary>
        /// 带回传函数的添加编辑删除提示
        /// </summary>
        /// <param name="msgtitle">提示文字</param>
        /// <param name="url">返回地址</param>
        /// <param name="callback">JS回调函数</param>
        protected void JscriptMsg(string msgtitle, string url, string callback)
        {
            string msbox = "parent.jsprint(\"" + msgtitle + "\", \"" + url + "\", " + callback + ")";
            ClientScript.RegisterClientScriptBlock(Page.GetType(), "JsPrint", msbox, true);
        }
        #endregion


        /// <summary>
        /// 获得Repeate中被选中的ids
        /// </summary>
        /// <param name="repeater"></param>
        /// <returns></returns>
        public string GetCheckedIds(Repeater repeater)
        {
            string ids = "";
            for (int i = 0; i < repeater.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)repeater.Items[i].FindControl("HiddenFieldId")).Value);
                CheckBox cb = (CheckBox)repeater.Items[i].FindControl("CheckBoxId");
                if (cb.Checked)
                {
                    ids = ids + id.ToString() + ",";
                }
            }
            ids = Utils.RemoveTheLastCharacte(ids);
            return ids;
        }

    }
}
